import json

import requests
from app.core.conf import ST
from loguru import logger
from sche_task import schedulerIns
from sio import sio


def register_temp_humi_event():
    # all1
    try:
        schedulerIns.remove_job("temp_humi")
        schedulerIns.add_job(get_temp_and_humi, "interval", seconds=2, args=[], id="temp_humi")
    except Exception as e:
        logger.error("暂时没有temp_humi的任务，直接添加")
    return


async def get_temp_and_humi():
    try:
        r = requests.get(ST.TEMP_HUMI_URL)
        await sio.emit("temp_humi", eval(r.text))
    except Exception as e:
        logger.debug("无法温湿度，请检查相关硬件设备")
